<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 提现
 */
class WithdrawalsController extends CommonController {

    public function index()
    {
        $wd = I('wd');
        $map = [];
        if($wd){
            $map['bank_no'] = ['like', "%{$wd}%"];
        }
        list($list, $page) = $this->lists('Withdrawals', $map, 'id desc');
        if($list){
            $status_arr = C('WITHDRAWALS_STATUS');
            if($user_ids = array_column($list, 'user_id')){
                $user_arr = D('user')->where(['id'=>['in', $user_ids]])->getField('id,nickname,phone');
            }else{
                $user_arr = [];
            }
            $source_arr = ['1'=>'分销提现', '2'=>'推广下线等级提现', '3'=>'抵用券提现'];
            foreach ($list as $key => &$row) {
                if(isset($user_arr[$row['user_id']])){
                    $row['nickname'] = $user_arr[$row['user_id']]['nickname'];
                    $row['phone'] = $user_arr[$row['user_id']]['phone'];
                }else{
                    $row['nickname'] = '';
                    $row['phone'] = '';
                }
                $row['source_name'] = isset($source_arr[$row['source']]) ? $source_arr[$row['source']] : '';
                $row['status_name'] = isset($status_arr[$row['status']]) ? $status_arr[$row['status']] : '';
            }
        }
        $this->assign('page',$page);
        $this->assign('list', $list);
        $this->display();
    }

    public function edit()
    {
        $id = I('id');
        $bank_codes = withdrawals_bank_codes();
        $info = M('Withdrawals')->where(['id'=>$id])->find();
        if(IS_POST){
            if($info['status']!=1){
                $this->ajaxReturn(array('s'=>false,'m'=>'已审核过，无法修改'));
            }
            $data = I('post.');
            if(empty($data['bank_code'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择银行卡类型'));
            }
            if(!in_array($data['bank_code'], array_keys($bank_codes))){
                $this->ajaxReturn(['s'=>false, 'm'=>'银行卡类型暂不支持']);
            }
            $data['bank_name'] = $bank_codes[$data['bank_code']];
            if(empty($data['bank_no'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入银行卡'));
            }
            if(empty($data['amount'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入提现金额'));
            }
            $min_withdrawal_amount = yzl_config('withdrawals.withdrawals_min_amount');
            $max_withdrawal_amount = yzl_config('withdrawals.withdrawals_max_amount');
            if($data['amount'] < $min_withdrawal_amount){
                $this->ajaxReturn(['s'=>false, 'm'=>'最低提现金额'.$min_withdrawal_amount]);
            }
            if($data['amount'] > $max_withdrawal_amount){
                $this->ajaxReturn(['s'=>false, 'm'=>'最高提现金额'.$max_withdrawal_amount]);
            }

            $model = M('Withdrawals');
            $data['update_time'] = time();
            $model->startTrans();
            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                switch ($info['source']) {
                    case '1':// 分销提现
                        if(isset($data['status'])){
                            switch ($data['status']) {
                                case '2'://同意
                                    // $tx_res = pay_bank(1, $id, $data['bank_code'], $data['bank_no'], $data['user_name'], $data['amount']);
                                    // if($tx_res['status'] == 1){
                                    //     $model->commit();
                                    //     $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
                                    // }else{
                                    //     $model->rollback();
                                    //     $this->ajaxReturn(array('s'=>false,'m'=>$tx_res['msg']));
                                    // }
                                    break;
                                case '3'://拒绝
                                    money_log($info['user_id'], $info['amount'], 3, $id);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case '3':// 抵用券
                        if(isset($data['status'])){
                            if($data['status'] == 2){
                                M('vouchers')->where(['code'=>$info['source_id']])->save(['status'=>4, 'update_time'=>time()]);
                            }else if($data['status'] == 3){
                                M('vouchers')->where(['code'=>$info['source_id']])->save(['status'=>5, 'update_time'=>time()]);
                            }
                        }
                        break;
                    default:
                        break;
                }
                $model->commit();
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
            }else{
                $model->rollback();
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $this->assign('info', $info);
            $this->assign('bank_codes', $bank_codes);
            $this->display();
        }
    }

    public function del()
    {
        $id = I('id');
        $info = M('Withdrawals')->where(['id'=>$id])->find();
        if(empty($info)){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }
        if($info['status']==1){
            $this->ajaxReturn(array('s'=>false,'m'=>'未审核，无法删除'));
        }
        $res = M('Withdrawals')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }
}